<?php
namespace  Admin\Controller;
use        Think\Controller;
class HelpTypeController extends CommonController{
                //分类列表
                function index()
                {
                    $model = D('HelpType');
                    $list = $model->getCateTree();
                    $this->assign('userid',session('userid'));
                    $this->assign('list',$list);
                    $this->display();

                }
                //显示和添加分类
                public function add(){
                    $model = D('HelpType');
                    $list = $model->getCateTree();
                    $this->assign('list',$list);

                    if(IS_POST){
                        $model = D('HelpType');
                        //自动验证
                        $data =$model->create();
                        if(!$data){
                            $this->error($model->getError());
                        }else{
                            $insertid = $model->add($data);
                            $res=$model->field("id")->where(array("title"=>$data['title']))->find();
                            $map['content'] = $_POST['content'];
                            $map['tid']=$res['id'];
                            $resl=M("help_content")->add($map);
                        }

                        if(!$insertid && !$resl){
                            $this->error('数据写入失败');
                        }
                        $this->success('写入成功',"",1);die;
                    }
                    $this->display();
    }

        //编辑
            public function edit(){
                if(IS_GET){
                    //显示要编辑的分类信息
                    $id=intval(I('get.id'));
                    //根据ID参数获取该分类的信息
                    $model = D('HelpType');
                    $info = $model->findOneById($id);
                    $this->res=M("Help_content")->field('content')->where(array("tid"=>$info['id']))->find();
                    //获取所有的分类信息
                    $cate = $model->getCateTree();
                    //判断所选类别属于哪个分类id
                    foreach ($cate as $k=>$v){
                        if ($v['id']==$info['pid']){
                            $id=$k+1;
                        }
                    }
                        if ($info['pid']==0){
                            $id=0;
                        }
//                    dump($cate);
//                    dump($info);
//                    dump($id);
//                    die;
                    $this->assign('cate',$cate);
                    $this->assign('info',$info);
                    $this->assign('id',$id);
                    $this->display();
                }else{
                    //实现数据修改
                    $model =D('HelpType');
                    $data = $model->create();
                    $res = $model->update($data);
                    if($res===false){
                        $this->error($model->getError());
                    }
                    $map['content']=$_POST['content'];
                    $map['tid']=I("post.id");
                    if($res=M("help_content")->field("id")->where(array("tid"=>$map['tid']))->find()){
                        $map["id"]=$res['id'];
                        M("help_content")->save($map);
                        $this->success('修改成功',U('index'));
                    }
                }
             }
             public function dels(){
                $id=I("get.id");
                $model=D('HelpType')->dels($id);
                if($model){
                    M("help_content")->where(array("tid"=>$id))->delete();
                    $this->success("删除成功","",1);
                }else{
                    $this->error("分类下有子类不允许删除","",1);
                }
                // 判断删除的分类下,有无下级分类存在.
//                 $model=D('HelpType')->getChildren($id);
//                 if(empty($model)){
//                    echo "可以删除";
//                 }else{
//                     echo "不能删除";
//                 }
             }
    }

?>